{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0a87ba07",
   "metadata": {},
   "source": [
    "## 关于numpy数据类型的演示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f92fe396",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "085ece23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "#创建numpy数组时不指定数据类型，会自动匹配相应类型\n",
    "a = np.arange(10)\n",
    "print(a)\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e32ba918",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "int8\n"
     ]
    }
   ],
   "source": [
    "#指定int8数据类型，使用参数dtype,int8只能存储最大整数至127\n",
    "b = np.array([1,2,3,4],dtype=np.int8)\n",
    "print(b)\n",
    "print(b.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f5c2f218",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4.]\n",
      "float16\n"
     ]
    }
   ],
   "source": [
    "#指定float16数据类型，使用参数dtype\n",
    "c = np.array([1,2,3,4],dtype=np.float16)\n",
    "print(c)\n",
    "print(c.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "35d99b2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<__main__.Person object at 0x7fa03b9cfe20>\n",
      " <__main__.Person object at 0x7fa03b9cf790>]\n",
      "object\n"
     ]
    }
   ],
   "source": [
    "#也可以制定object数据类型（或默认匹配出），对应的是python类等对象\n",
    "class Person():\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "d = np.array([Person('小名',10),Person('小刚',39)])\n",
    "print(d)\n",
    "print(d.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "22cfe46d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'a' b'b' b'c']\n",
      "|S1\n"
     ]
    }
   ],
   "source": [
    "#对应python中的byte字符串类型\n",
    "e = np.array(['a','b','c'],dtype='S')\n",
    "print(c)\n",
    "print(c.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4df4cfe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 'b' 'c']\n",
      "<U25\n"
     ]
    }
   ],
   "source": [
    "#对应python中的unicode字符串类型。正常显示的字符串。\n",
    "f = np.array(['a','b','c'],dtype='U25')\n",
    "print(f)\n",
    "print(f.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8c99bd8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 'b' 'c']\n",
      "<U1\n",
      "|S1\n"
     ]
    }
   ],
   "source": [
    "eu = e.astype('U')\n",
    "print(eu)\n",
    "print(eu.dtype)\n",
    "print(e.dtype)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
